-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local enum = require 'class.types.types'
local responder_collection = require 'domain.responder_collection'

local spdm_mock = class()

local MDB_NAME = "SPDM_0"
local MOCK_DEVICE_NAME = "test"
local MOCK_TYPE = "Discrete"
local MOCK_PATH_FMT = "/bmc/kepler/Systems/%s/Storage/Controllers/%s"
local MOCK_CHASSIS_ID = 1

function spdm_mock:ctor()
    self.m_responder_collection = responder_collection.new()
end

-- 打桩将资源都上树
function spdm_mock:mock_for_it(spdm_c)
    -- 创建 Responder
    local rpc_path = string.format(MOCK_PATH_FMT, tostring(MOCK_CHASSIS_ID), MOCK_DEVICE_NAME)
    local obj = {
        device_name = MOCK_DEVICE_NAME,
        mdb_name    = MDB_NAME
    }

    self.m_responder_collection:new_responder(MOCK_CHASSIS_ID, obj)
    self.m_responder_collection:new_component(MOCK_CHASSIS_ID, obj, rpc_path, MOCK_TYPE)

    -- 创建上下文
    spdm_c:new_spdm_ctx(MOCK_DEVICE_NAME)

    -- 初始化上下文
    spdm_c:init_spdm_ctx(MOCK_DEVICE_NAME, enum.TRANSFER_TYPE.MCTP:value())

    -- 初始化连接
    spdm_c:init_connection(MOCK_DEVICE_NAME)

    -- 调用GET_DIGEST
    spdm_c:get_digests(MOCK_DEVICE_NAME)

    -- 调用GET_CERTIFICATE
    local slot_id = 0
    local cert_info = spdm_c:get_certificate(MOCK_DEVICE_NAME, slot_id)
    self.m_responder_collection:add_certificate(MOCK_CHASSIS_ID, MOCK_DEVICE_NAME, slot_id, cert_info)

    -- 初始化mesurements
    spdm_c:init_measurements(MOCK_DEVICE_NAME, 0)

    -- 获取component_integrity和measurement_set
    local integrity = spdm_c:get_component_integrity(MOCK_DEVICE_NAME)
    local measurement_set = spdm_c:get_measurement_set(MOCK_DEVICE_NAME);
    local integrity_id =
        self.m_responder_collection:new_integrity(MOCK_CHASSIS_ID, MOCK_DEVICE_NAME, integrity, measurement_set)

    -- 获取measurement
    local measurement
    for i = 1, 3 do
        measurement = spdm_c:get_one_measurement(MOCK_DEVICE_NAME, i);
        self.m_responder_collection:add_measurement(MOCK_CHASSIS_ID, MOCK_DEVICE_NAME, integrity_id, measurement)
    end
end

return spdm_mock